package com.canbot.u05.activity.reception;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.HashMap;

/**
 * 欢迎文字设置界面
 */
public class ReceptionModeSettingTextActivity extends ReceptionBaseActivity implements View.OnClickListener {

        private TextView tvTitle;

        private ImageView ivBack, btnSure, btnCancle;

        private EditText editText;

        private ToastTools toastTools;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_reception_mode_setting_text);
                toastTools = ToastTools.newInstance(this);
                MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.USHER_DIR_PATH + "hywzsz_ybms_u05_001.wav");
                setViews();
        }

        private void setViews() {
                tvTitle = (TextView) findViewById(R.id.textView_title);
                ivBack = (ImageView) findViewById(R.id.imageView_back);
                btnCancle = (ImageView) findViewById(R.id.iv_usher_mode_text_imageView_cancle);
                btnSure = (ImageView) findViewById(R.id.iv_usher_mode_text_imageView_imageView_sure);
                editText = (EditText) findViewById(R.id.editText_text);
                tvTitle.setText("欢迎文字设置");
                btnCancle.setOnClickListener(this);
                btnSure.setOnClickListener(this);
                ivBack.setOnClickListener(this);
                editText.addTextChangedListener(watcher);
                String tts = getIntent().getStringExtra("tts");
                editText.setText(tts);
                editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                        @Override
                        public void onFocusChange(View v, boolean hasFocus) {
                                if (hasFocus) {
                                        editText.setSelection(editText.length());
                                }
                        }
                });
        }

        private TextWatcher watcher = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (s.length() > 50) {
                                editText.setText(s.subSequence(0, 50));
                                editText.setSelection(50);
                                toastTools.showText(getString(R.string.input_up_words4));
                        }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
        };
        private Dialog dialogLoading;
        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.imageView_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                        case R.id.iv_usher_mode_text_imageView_cancle:
                                //取消
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                        case R.id.iv_usher_mode_text_imageView_imageView_sure:
                                //确定
                                String trim = editText.getText().toString().trim();
                                if (TextUtils.isEmpty(trim)) {
                                        toastTools.showText(getString(R.string.text_cannot_empty));
                                        return;
                                }
                                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
                                updateWelTTs(trim);
                                break;
                }
        }
        private void updateWelTTs(final String data) {
                String url = IpMsgConst.HTTP_URL_CHEST + "usherModeAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", 1);
                hashMap.put("data", data);
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                toastTools.showText(getString(R.string.upload_complete));
                                Intent intent = new Intent();
                                intent.putExtra("tts",data);
                                setResult(RESULT_OK,intent);
                                finish();
                        }

                        @Override
                        public void onFailure() {
                                toastTools.showText(getString(R.string.network_error));
                                LoadingDialogUtils.closeDialog(dialogLoading);
                        }
                });
        }

}
